package kaoshi.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static String driver;
    private static String ur1;
    private static String user;
    private static String pwd;

    protected Connection conn = null;
    protected PreparedStatement pstmt = null;
    protected ResultSet rs = null;

    //静态块  程序启动  自动调用
    static {
        init();
    }

    //编写方法 从配置文件中读取连接属性的值
    public static void init(){
        try {
            //创建一个对象 用来获取配置文件中的属性值
            Properties pro = new Properties();
            //使用流 把配置文件读取到
            String config = "database.properties";//配置文件路径
            InputStream in = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
            pro.load(in);//把读取到的配置文件放入对象中
            //通过对象 拿到配置文件中的属性值8
            driver = pro.getProperty("driver");
            ur1 = pro.getProperty("ur1");
            user = pro.getProperty("user");
            pwd = pro.getProperty("pwd");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //创建连接方法
    //一根创建好的连接对象
    public Connection getConn(){
        try {
            Class.forName(driver);//加载驱动
            conn = DriverManager.getConnection(ur1,user,pwd);//建立连接
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    //关闭连接
    public void closeAll(ResultSet rs,PreparedStatement pstmt,Connection conn){
        try {
            if (null!=rs){
                rs.close();
            }
            if (null!=pstmt){
                pstmt.close();
            }
            if (null!=conn){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    //增删改操作
    //替换占位符
    //return 受影响的行数
    //删除
    public int executeUpdate(String sql,Object[] params){
        int count = 0;
        try {
            conn = this.getConn();//获取连接
            pstmt = conn.prepareStatement(sql);
            if (params!=null){
                for (int i = 0;i<params.length;i++){
                    pstmt.setObject((i+1),params[i]);
                }
            }
            count = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            this.closeAll(null,pstmt,conn);
        }
        return count;
    }

    //查询
    public ResultSet executeQuery(String sql,Object[] params){
        PreparedStatement pstmt = null;
        try {
            conn = getConn();//获取连接
            pstmt = conn.prepareStatement(sql);
            if (null!=params){
                for (int i = 0;i<params.length;i++){
                    pstmt.setObject((i+1),params[i]);
                }
            }
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
}
